<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2020/8/4
 * Time: 23:23
 */

namespace app\models\store;


use app\models\shop\MerchantAdmin;
use app\models\user\User;
use crmeb\basic\BaseModel;
use think\Db;

class StoreServiceList extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'store_service_list';

    /**
     * 添加联系
     * @param $uid  //当前用户id
     * @param $to_uid  //发送用户id
     * @param $type  //发送者身份 1=>商户端  2=>用户端
     * @return bool
     */
    public static function add($uid,$to_uid,$type=1)
    {
        Db::startTrans();
        try{
            //发送者
            $send = self::where(['uid'=>$uid,'to_uid'=>$to_uid])->find();
            if($send){  //存在则更新状态和时间
                $send->status = 1;
                $send->type   = $type;
                $send->updated_at = date('Y-m-d H:i:s');
                $res1 = $send->save();
            }else{
                //添加记录
                $res1 = self::insertGetId([
                    'uid' => $uid,
                    'to_uid' => $to_uid,
                    'type'   => $type,
                    'created_at'=>date('Y-m-d H:i:s',time())
                ]);
            }
            //接收者
            $receive = self::where(['uid'=>$to_uid,'to_uid'=>$uid])->find();
            if($receive){
                $receive->status = 1;
                $receive->type = $type;
                $receive->updated_at = date('Y-m-d H:i:s');
                $res2 = $send->save();
            }else{
                $res2 = self::insertGetId([
                    'uid' => $to_uid,
                    'to_uid' => $uid,
                    'type'   => $type,
                    'created_at'=>date('Y-m-d H:i:s',time())
                ]);
            }
            if(!$res1 || !$res2){
                Db::rollback();
            }
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
        return true;
    }

    /**
     * 联系人列表
     * @param $uid  当前用户id
     * @param $page
     * @param $limit
     * @return mixed
     */
    public static function list($uid,$page,$limit,$type=1)
    {
        $data = self::where('uid',$uid)->where('type',$type)->page($page,$limit)->select()->each(function ($item) {
            // 发送者为商户端
            if($item['type'] == 1){
                $userInfo = User::where('uid', $item['to_uid'])->field('nickname,avatar')->find();
                $item['nickname'] = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
                $item['avatar']   = isset($userInfo['avatar']) ? $userInfo['avatar'] : '';
            }else{
                //发送者为用户端
                $userInfo = MerchantAdmin::where('id', $item['to_uid'])->field('real_name as nickname')->find();
                $item['nickname'] = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
                $item['avatar']   = isset($userInfo['avatar']) ? $userInfo['avatar'] : '';

            }
        });
        return $data = count($data) ? $data->toArray() : [];
    }
    
    /***
     * @param $id
     * @return bool
     * @throws \Exception
     */
    public static function del($id,$uid)
    {
        $res = self::where('id',$id)->where('uid',$uid)->update(['status' => 0]);
        return $res;
    }
}